<?php

namespace WeForge\Queue;

use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\Job as JobContract;
use Illuminate\Queue\Jobs\Job;

class FnfJob extends Job implements JobContract
{
    /**
     * The fnf client.
     *
     * @var \HttpClient\Aliyun\ServerlessWorkflow\Application
     */
    protected $fnf;

    /**
     * The job message.
     *
     * @var array
     */
    protected $message;

    /**
     * Create a new Job instance.
     * @param \Illuminate\Contracts\Container\Container $container
     * @param \WeForge\Queue\FnfQueue     $fnf
     * @param array     $message
     * @return  void
     */
    public function __construct(Container $container, FnfQueue $fnf, array $message)
    {
        $this->container = $container;
        $this->fnf = $fnf;
        $this->message = $message;
        $this->connectionName = 'fnf';
    }

    /**
     * Get the job identifier.
     *
     * @return string
     */
    public function getJobId()
    {
        return '';
    }

    /**
     * Get the raw body of the job.
     *
     * @return string
     */
    public function getRawBody()
    {
        return $this->message['payload'];
    }

    /**
     * Get the number of times the job has been attempted.
     *
     * @return int
     */
    public function attempts()
    {
        return ($this->message['attempts'] ?? null) + 1;
    }

    /**
     * Release the job back into the queue.
     *
     * Accepts a delay specified in seconds.
     *
     * @param  int  $delay
     * @return void
     */
    public function release($delay = 0)
    {
        parent::release($delay);

        $this->fnf->release(null, array_merge($this->message, ['attempts' => $this->attempts()]), $delay);
    }
}
